import Cookies from 'js-cookie'

import md5 from 'js-md5'

const TokenKey = 'token'

export function getToken() {
    return Cookies.get(TokenKey)
}

export function setToken(token, expires) {
    expires = expires || {}
    return Cookies.set(TokenKey, token, expires)
}

export function removeToken() {
    return Cookies.remove(TokenKey)
}

export const viewFullScreen = function () {
    // 主要，这边要全屏的对象是这个body，如果只是选择点击的对象，比如 e.currentTarget，那么就会变成只有这个按钮全屏
    var docElm = document.documentElement;
    // 浏览器兼容
    if (docElm.requestFullscreen) {
        docElm.requestFullscreen();
    } else if (docElm.msRequestFullscreen) {
        docElm.msRequestFullscreen();
    } else if (docElm.mozRequestFullScreen) {
        docElm.mozRequestFullScreen();
    } else if (docElm.webkitRequestFullScreen) {
        docElm.webkitRequestFullScreen();
    }
}

export const cancelFullScreen = function () {
    // 浏览器兼容
    if (document.exitFullscreen) {
        document.exitFullscreen();
    } else if (document.msExitFullscreen) {
        document.msExitFullscreen();
    } else if (document.mozCancelFullScreen) {
        document.mozCancelFullScreen();
    } else if (document.webkitCancelFullScreen) {
        document.webkitCancelFullScreen();
    }
}

export const dateFormat =  (date, fmt) => {
    let o = {
        'M+': date.getMonth() + 1, // 月份
        'd+': date.getDate(), // 日
        'h+': date.getHours() % 12 === 0 ? 12 : date.getHours() % 12, // 小时
        'H+': date.getHours(), // 小时
        'm+': date.getMinutes(), // 分
        's+': date.getSeconds(), // 秒
        'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
        'S': date.getMilliseconds() // 毫秒
    };
    let week = {
        '0': '/u65e5',
        '1': '/u4e00',
        '2': '/u4e8c',
        '3': '/u4e09',
        '4': '/u56db',
        '5': '/u4e94',
        '6': '/u516d'
    };
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    if (/(E+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? '/u661f/u671f' : '/u5468') : '') + week[date.getDay() + '']);
    }
    for (let k in o) {
        if (new RegExp('(' + k + ')').test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)));
        }
    }
    return fmt;
}

export const getBetweenDateStr = (start,end) => {
    let result = [];
    let beginDay = start.split("-");
    let endDay = end.split("-");
    let diffDay = new Date();
    let dateList = [];
    let i = 0;
    diffDay.setDate(beginDay[2]);
    diffDay.setMonth(beginDay[1]-1);
    diffDay.setFullYear(beginDay[0]);
    result.push(start);
    while(i === 0){
        let countDay = diffDay.getTime() + 24 * 60 * 60 * 1000;
        diffDay.setTime(countDay);
        dateList[2] = diffDay.getDate();
        dateList[1] = diffDay.getMonth() + 1;
        dateList[0] = diffDay.getFullYear();
        if(String(dateList[1]).length === 1){dateList[1] = "0"+dateList[1]};
        if(String(dateList[2]).length === 1){dateList[2] = "0"+dateList[2]};
        result.push(dateList[0]+"-"+dateList[1]+"-"+dateList[2]);
        if(dateList[0] == endDay[0] && dateList[1] == endDay[1] && dateList[2] == endDay[2]){
            i = 1;
        }
    }
    return result;
};

export const millisecondFormat = (value) => {
    let totalSeconds = Math.ceil(value / 1000)
    let second = 0, minutes = 0, hours = 0
    if (totalSeconds >= 60) {
        second = totalSeconds % 60
        minutes = Math.floor(totalSeconds / 60)
    } else {
        second = totalSeconds
        minutes = 0
    }
    if (minutes >= 60) {
        hours = Math.floor(minutes / 60)
        minutes = minutes % 60
    } else {
        hours = 0
    }
    return '' + (hours < 10 ? '0' + hours : hours) + ':' + (minutes < 10 ? '0' + minutes : minutes) + ':' + (second < 10 ? '0' + second : second)
}

export const passwordMd5 = function (password) {
    return md5(password)
}

export const setUrlDomain = function (url, imgUrlDomain) {
    if (!url) return url;
    const root_domain = /^\w+\:\/\/\/?[^\/]+/.exec(url)[0]
    const index = url.indexOf(root_domain) + root_domain.length
    const relativeUrl = url.substr(index)
    return imgUrlDomain + relativeUrl
}
